import axios from '@/utils/axios';
import configService from '@/utils/config';
const { post, get } = axios;
const host = configService.Host1; // 正式环境

export class RravelServices{
  private addEntity = 'tracks/addEntity.json'; // 新增设备，只能POST请求
  private addpoint = 'tracks/addTracks.json'; // 批量添加轨迹点
  private service_id = '230309'; // 在轨迹管理台创建鹰眼服务时，系统返回的 service_id
  private getlatestpoint = 'tracks/tracksRectification.json'; // 获取纠偏后的经纬度
  private ak = 'jPHKARpA5ARSvY8GTt5VThdvB4THpSdC'; // 百度ak
  public entity_name = configService.loginInfo.userId; // 	entity名称，作为其唯一标识
  /**
   * 百度新增终端
   */
  addEntityFn(): Promise<any>{
    return post( host + this.addEntity, {
      service_id: this.service_id,
      ak: this.ak,
      entity_name: this.entity_name
    }, false, 'application/json');
  }

  /**
   * 百度上传轨迹
   * @param point 
   */
  addpointsFn(point: Array<any>, callback: any){
    // let point_list = point;
    post( host + this.addpoint +  '?app_token=' + configService.token, {
      service_id: this.service_id,
      ak: this.ak,
      point_list: JSON.stringify(point)
    }, false).then((res: any) => {
      // console.log('addpointsFn', res)
      if(res !== 'error' && res.data.status * 1 === 0){
        // this.getlatestpointFn(point_list, callback);
        callback(res);
      } else {
        callback('error');
      }
    });
  }

  /**
  * getlatestpoint——百度实时纠偏
  */
  getlatestpointFn(point_list: Array<any>, showloaind: boolean, callback: any) {
    post( host + this.getlatestpoint +  '?app_token=' + configService.token, {
      ak: this.ak,
      service_id: this.service_id,
      entity_name: this.entity_name,
      is_processed: 1,
      supplement_mode: 'no_supplement',
      coord_type_output: 'gcj02',
      page_size: 5000,
      page_index: 1,
      end_time: point_list.length > 0 ? point_list[point_list.length - 1].loc_time : '', // 1636697856, // point_list[point_list.length - 1].loc_time,
      start_time: point_list.length > 0 ? point_list[0].loc_time : '', // 1636697361, //point_list[0].loc_time,
      process_option: 'denoise_grade=1,need_mapmatch=1,transport_mode=auto'
    }, showloaind).then((res: any) => {
      if(res !== 'error' && res.data.status * 1 === 0){
        callback(res);
      } else {
        callback('error');
      }
  });
}
}